<template>
  <div class="login">
<!-- 

  名门望族 张琼宇  登录页面

 -->
    <div class="box"></div>
    <div class="box1">
      <div class="box2">
        <img src="../assets/logo.png" alt="">
      </div>
      <div class="form">
        <el-form ref="ruleForm" :model="ruleForm" :rules="rules">
          <el-form-item prop="username">
            <el-input v-model="ruleForm.username">
              <i slot="prefix" class="el-icon-user-solid">
              </i>
            </el-input>
          </el-form-item>
          <el-form-item prop="password">
            <el-input v-model="ruleForm.password">
              <i slot="prefix" class="el-icon-s-goods">
              </i>
            </el-input>
          </el-form-item>
          <el-form-item style="float: right;">
            <el-button type="primary" @click="submitForm('ruleForm')">登录</el-button>
            <el-button @click="resetForm('ruleForm')">重置</el-button>
          </el-form-item>
        </el-form>
      </div>
    </div>
  </div>
</template>

<script>
import {adduser} from '@/utils/loginapi'
export default {
  data() {
    return {
      ruleForm: {
        username: 'admin',
        password: '123456'
      },
      rules: {
        username: [
          { required: true, message: '请输入账号', trigger: 'blur' },
          { min: 3, max: 5, message: '长度在 3 到 5 个字符', trigger: 'blur' }
        ],
        password: [
          { required: true, message: '请输入密码', trigger: 'blur' },
        ],
      },
    }
  },
  methods: {
    submitForm(formName) {
      // 点击确定按钮效验规则
      this.$refs[formName].validate(async (valid) => {
        if (valid) {
          let res = await adduser(this.ruleForm)
          if (res.meta.status == 200) {
            // 效验成功
            this.$message.success(res.meta.msg)
            // 将后台返回的token存入到 sessionStorage 中
            sessionStorage.setItem('token', res.data.token)
            this.$router.push('/home')
          } else {
            // 效验失败
            this.$message.error(res.meta.msg)
          }
        }
      });
    },
    resetForm(formName) {
      this.$refs[formName].resetFields();
    }
  },
}
</script>

<style lang="scss" scoped>
.login {
  width: 100%;
  height: 100%;
  position: relative;
  .box {
    width: 100%;
    height: 100%;
    position: absolute;
    background: rgb(50, 74, 104);
  }
  .box1 {
    position: absolute;
    left: 50%;
    top: 50%;
    width: 500px;
    height: 300px;
    transform: translate(-50%, -50%);
    background: #fff;
    .box2 {
      position: absolute;
      width: 200px;
      height: 200px;
      background: rgb(239, 238, 238);
      left: 50%;
      top: 0px;
      transform: translate(-50%, -50%);
      border-radius: 50%;
      box-shadow: 0 0 10px #fff;
      img {
        width: 100%;
        border-radius: 50%;
        border: 10px solid #fff;
      }
    }
    .form {
      padding: 30px;
      margin-top: 80px;
      // .el-input {
      //   margin-bottom: 15px;
      // }
    }
  }
}
</style>